package app_res

import (
	"errors"
	"fmt"
	"hft/Database/admin"
	join_db "hft/Database/admin/join"
	"hft/Http/Services/front/app/app_dao"
	"hft/Http/Services/front/app/app_model"
	"hft/e"
	"hft/global"
	"hft/tools"

	"gorm.io/gorm"
)

// 移动端
type Application struct{}

// 获取首页
func (*Application) Index(city int) (data *app_model.Index, err error) {
	regionLink, err := app_dao.AppBaseDao.GetRegion(global.OC_DB, city)
	if err != nil {
		return nil, err
	}
	newlist := app_dao.AppIndexDao.GetArticle(global.OC_DB, regionLink)      //今日资讯
	recommend := app_dao.AppIndexDao.GetLouPanList(global.OC_DB, regionLink) //推荐楼盘
	newLou := app_dao.AppIndexDao.GetLouPanNewList(global.OC_DB, regionLink) //新房
	emojiList := app_dao.AppIndexDao.GetEmojiList(global.OC_DB)
	var index = &app_model.Index{
		News:            newlist,
		RecommendLouPan: recommend,
		NewLouPan:       newLou,
		Emoji:           emojiList,
	}
	return index, nil
}

/* 换取城市id */
func (*Application) TransSiteID(city_name string) (*app_model.TransSID, error) {
	var transCity = &app_model.TransSID{Sid: 0, Name: ""}
	var region *admin.Region
	err := global.OC_DB.Select("name,id,alias").Where("name = ?", city_name).Or("alias = ?", city_name).First(&region).Error
	fmt.Println(tools.JsonParse(region))
	if err != nil {
		return nil, errors.New(e.RegionNotFound)
	}
	transCity.Sid = int(region.ID)
	if region.Alias == "" {
		transCity.Name = region.Name
	} else {
		transCity.Name = region.Alias
	}
	fmt.Println("1111", transCity)
	return transCity, nil
}

// 获取省份以及城市
func (*Application) GetProvinceAndCity() *app_model.JoinConfig {
	var region []*admin.Region
	var countryConf *admin.CountrySetting
	var config = &app_model.JoinConfig{
		RegionList: []*app_model.ProvinceCityBody{},
		Phone:      "",
	}
	global.OC_DB.Where("level = ?", "province").Preload("Districts", func(db *gorm.DB) *gorm.DB {
		return db.Order("sort desc")
	}).Find(&region)
	// fmt.Println(tools.JsonParse(region[0].Districts))
	for _, v := range region {
		var p_name string
		if v.Alias != "" {
			p_name = v.Alias
		} else {
			p_name = v.Name
		}
		var newItem = &app_model.ProvinceCityBody{Name: p_name, ID: v.ID, FirstWord: v.FirstWord, Level: v.Level}
		for _, j := range v.Districts {
			var c_name string
			if j.Alias != "" {
				c_name = j.Alias
			} else {
				c_name = j.Name
			}
			var newChildItem = &app_model.ProvinceCityBody{Name: c_name, ID: j.ID, FirstWord: j.FirstWord, Level: j.Level}
			newItem.Children = append(newItem.Children, newChildItem)
		}
		config.RegionList = append(config.RegionList, newItem)
	}
	global.OC_DB.Select("fran_phone").First(&countryConf)
	config.Phone = countryConf.FranPhone
	return config
}

// 加盟合作
func (*Application) JoinUs(data *app_model.JoinUsBody) error {
	err := tools.AliyunSmsTool.VerifyValid(global.OC_CONFIG.AliyunSms.UserSend, data.Phone, data.Code)
	if err != nil {
		return errors.New(e.CodeInValid)
	}
	var newData = &join_db.Join{
		Phone:    data.Phone,
		Username: data.Username,
		Company:  data.Company,
		Region:   data.Region,
	}
	if err := global.OC_DB.Where("phone = ?", data.Phone).FirstOrCreate(&newData).Error; err != nil {
		return errors.New(e.ErrorInfo)
	} else {
		return nil
	}
}

// 获取用户协议
func (*Application) GetAssistAndPrivate() *app_model.GetAssistAndPrivateBody {
	var assist *admin.SmallCategory
	var private *admin.SmallCategory
	global.OC_DB.Preload("Articles").First(&assist, 8)
	global.OC_DB.Preload("Articles").First(&private, 9)
	var body = &app_model.GetAssistAndPrivateBody{
		Assist:  assist,
		Private: private,
	}
	return body
}

var ApplicationService = new(Application)
